package com.jpcc.vw.onlineRadio.activity;

import android.content.Intent;
/*Start by menglingqiang*/
import android.util.Log;
/*End by menglingqiang*/
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
/*Start by menglingqiang*/
import com.jpcc.vw.onlineRadio.application.MusicApplication;
/*End by menglingqiang*/
import com.jpcc.vw.onlineRadio.base.BaseActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.IRadio;
import com.jpcc.vw.onlineRadio.model.bean.BaseBean;
import com.jpcc.vw.onlineRadio.model.bean.VIPInfoBean;
import com.jpcc.vw.onlineRadio.model.bean.VIPRecommendationBean;
import com.jpcc.vw.onlineRadio.model.impl.RadioImpl;
import com.jpcc.vw.onlineRadio.utils.CommonSubscriber;
import com.jpcc.vw.onlineRadio.utils.PreferenceUtils;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;
import com.orhanobut.logger.Logger;
/*Start by menglingqiang*/
import com.jpcc.vw.tracelog.TracelogInterface;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
/*End by menglingqiang*/

import java.util.ArrayList;
/*Start by menglingqiang*/
import java.util.Date;
/*End by menglingqiang*/
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

public class VIPRecommendationActivity extends BaseActivity {


    @BindView(R.id.layout_back)
    RelativeLayout layout_back;
    @BindView(R.id.list_view_vip_recommendation)
    ListView list_view_vip_recommendation;
    @BindView(R.id.layout_search)
    RelativeLayout layout_search;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_show_empty)
    TextView tv_show_empty;
    @BindView(R.id.imv_top_bottom_line)
    ImageView imv_top_bottom_line;
    @BindView(R.id.swipe_refrsh_tracks)
    SmartRefreshLayout swipe_refrsh_tracks;
    @BindView(R.id.progress_circular)
    ProgressBar progress_circular;
    @BindView(R.id.imv_new_search)
    ImageView imv_new_search;
    private IRadio model;
    private VIPRecommendationAdapter adapter;
    private List<VIPRecommendationBean.DataBean.PageDataBean> datas = new ArrayList<>();
    private boolean isUserVip=false;
    private boolean is_refresh=true;
    private int page_index=1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vip_recommendation;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        tv_title.setText(getResources().getString(R.string.string_62));
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VIPRecommendationActivity.this.finish();
            }
        });
        imv_new_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(VIPRecommendationActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });
        adapter = new VIPRecommendationAdapter();
        list_view_vip_recommendation.setAdapter(adapter);
        list_view_vip_recommendation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(VIPRecommendationActivity.this, AlbumDetailActivity.class);
                intent.putExtra("key_album_id", datas.get(position).getId());
//                intent.putExtra("isvipOnly",datas.get(position).isVipOnly());
                intent.putExtra("isvipFree",datas.get(position).isIs_vip_free());
                intent.putExtra("isSample",datas.get(position).isSample());
                intent.putExtra("HidePayMark",true);
                startActivity(intent);
            }
        });
        model = new RadioImpl();


        swipe_refrsh_tracks.setEnableRefresh(false);
        swipe_refrsh_tracks.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                is_refresh = true;
                swipe_refrsh_tracks.setEnableLoadmore(true);
                getVIPRecommendation();
            }
        });
        swipe_refrsh_tracks.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                is_refresh = false;
                getVIPRecommendation();
            }
        });
        is_refresh = true;
        getVIPRecommendation();
        model.QueryVIPInfo().subscribe(new CommonSubscriber<VIPInfoBean>() {
            @Override
            public void onSuccess(VIPInfoBean response) {
                if (response.getData() != null && response.getData().isVipFlag()) {
                    isUserVip=true;
                    PreferenceUtils.SaveVIPStatus(true);
                } else {
//                    ToastUtils.show(R.string.string_101);
                    isUserVip=false;
                    PreferenceUtils.SaveVIPStatus(false);
                }
            }

            @Override
            public void onFailure(BaseBean response) {
//                ToastUtils.show(response.getStatusMessage());
                isUserVip=false;
            }
        });
    }

    private void getVIPRecommendation(){
        int local_page = page_index;
        if (is_refresh) {
            local_page = 1;
        }
        model.VIPRecommendation(local_page).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
                if(is_refresh){
                    progress_circular.setVisibility(View.VISIBLE);
                }
            }
        }).subscribe(new CommonSubscriber<VIPRecommendationBean>() {
            @Override
            public void onSuccess(VIPRecommendationBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                swipe_refrsh_tracks.finishRefresh();
                swipe_refrsh_tracks.finishLoadmore();
                if (response.getData().getPageData() != null&&response.getData().getPageData().size()>0) {
                    tv_show_empty.setVisibility(View.GONE);
                    if (is_refresh) {
                        datas.clear();
                        page_index = 1;
                    }
                    datas.addAll(response.getData().getPageData());
                    adapter.notifyDataSetChanged();
                    if (datas.size() == 0&&is_refresh) {
                        tv_show_empty.setVisibility(View.VISIBLE);
                    }
                    is_refresh = false;
                    page_index=page_index+1;
                }
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),VIPRecommendationActivity.this.getClass().getName(),"DEBUG",response.getStatusCode().toString(),"VIPRecommendation onSuccess");
                    }else {
                        Log.e("JPCC","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                progress_circular.setVisibility(View.INVISIBLE);
                swipe_refrsh_tracks.finishRefresh();
                swipe_refrsh_tracks.finishLoadmore();
//                ToastUtils.show(response.getStatusMessage());
                tv_show_empty.setVisibility(View.VISIBLE);
                /*Start by menglingqiang*/
                Date dt = new Date();
                try {
                    TracelogInterface inter = MusicApplication.getLogInterface();
                    if(inter != null){
                        inter.sendLogData(dt.toString(),dt.toString(),VIPRecommendationActivity.this.getClass().getName(),"ERROR",response.getStatusCode().toString(),"VIPRecommendation onFailure");
                    }else {
                        Log.e("JPCC","inter == null");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /*End by menglingqiang*/
            }
        });
    }

    @Override
    public void doAtmosphere(boolean rgbOn, String color) {
        Logger.i("doAtmosphere" + "rgbOn-->" + rgbOn);
        Logger.i("doAtmosphere" + "color-->" + color);
        if (rgbOn) {
            if (TextUtils.isEmpty(color)) {
                imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
            } else {
                imv_top_bottom_line.setColorFilter(Color.parseColor(color));
            }
        } else {
            imv_top_bottom_line.setColorFilter(Color.TRANSPARENT);
        }
    }



    @Override
    public void setupData() {
        if (Constants.isRelease) {
            try {
//                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isAtmosphereLampOn();
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(this).isHmiAtmosphereLampOn();
//                String color = AtmosphereLampManager.getInstance(this).getAtmosphereLampColor();
                String color = AtmosphereLampManager.getInstance(this).getHmiAtmosphereLampColor();
                doAtmosphere(atmosphereStatus, color);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }


    class VIPRecommendationAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public VIPRecommendationBean.DataBean.PageDataBean getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_album_category, parent, false);
            }
            initViews(convertView, position);
            return convertView;
        }

        private void initViews(View view, int position) {
            TextView tv_category = ViewHolderUtils.get(view, R.id.tv_category);
            TextView tv_position = ViewHolderUtils.get(view, R.id.tv_position);
            ImageView imv_cover = ViewHolderUtils.get(view, R.id.imv_cover);
            ImageView imv_is_vip_mark=ViewHolderUtils.get(view,R.id.imv_is_vip_mark);
            if(datas.get(position).isIs_vip_free()){
                imv_is_vip_mark.setVisibility(View.VISIBLE);
            }
            else{
                imv_is_vip_mark.setVisibility(View.GONE);
            }
            tv_category.setText(datas.get(position).getTitle());
            Glide.with(VIPRecommendationActivity.this).load(datas.get(position).getCover().getLarge().getUrl()).into(imv_cover);
        }
    }
}
